Equality Trust에서 기부금을 받고 제공하는 두 종류의 자료 중 23개 국가의 각종 지표를 비교한 자료에 World Bank에서 발표하는 GDP자료를 추가하여 읽어들이면,
library(knitr)
# rm(list = ls())
# getwd()
load("Inequality_Index_HS.rda")
data.full <- read.csv("../data/international-inequality_GDP.csv", stringsAsFactors = FALSE)
str(data.full)
## 'data.frame': 23 obs. of 30 variables:
## $ Country : chr "Australia" "Austria" "Belgium" "Canada" ...
## $ Income.inequality : num 7 4.82 4.6 5.63 4.3 3.72 5.6 5.2 6.2 6.05 ...
## $ Trust : num 39.9 33.9 30.7 38.8 66.5 58 22.2 34.8 23.7 35.2 ...
## $ Life.expectancy : num 79.2 78.5 78.8 79.3 76.6 78 79 78.3 78.3 77 ...
## $ Infant.mortality : num 4.9 4.8 5 5.3 5.3 3.7 4.4 4.4 5 5.9 ...
## $ Obesity : num 18.4 14.5 13.5 12.8 15 ...
## $ Mental.illness : num 23 NA 12 19.9 NA NA 18.4 9.1 NA NA ...
## $ Maths.and.literacy.scores : num 524 498 518 530 503 ...
## $ Teenage.births : num 18.4 14 9.9 20.2 8.1 9.2 9.3 13.1 11.8 18.7 ...
## $ Homicides : num 16.9 11.6 13 17.3 12.7 28.2 21.5 13.7 13.9 8.6 ...
## $ Imprisonment..log. : num 4.61 4.52 4.28 4.77 4.17 4.11 4.5 4.51 3.33 4.17 ...
## $ Social.mobility : num NA NA NA 0.14 0.14 0.15 NA 0.17 NA NA ...
## $ Index.of.health...social_problems: num 0.07 0.01 -0.23 -0.07 -0.19 -0.43 0.05 -0.06 0.38 0.25 ...
## $ Child.overweight : num NA 11.9 10.4 19.5 10.3 13.3 11.2 11.3 16 12.1 ...
## $ Drugs.index : num 1.71 -0.02 -0.18 0.61 -0.09 -0.88 -0.35 -0.3 -0.99 -0.03 ...
## $ Calorie.intake : int 3142 3753 3632 3167 3405 3197 3576 3395 3687 3656 ...
## $ Public.health.expenditure : num 67.9 69.3 71.7 70.8 82.4 75.6 76 74.9 56 76 ...
## $ Child.wellbeing : num -0.21 -0.07 0.05 0.04 0.21 0.34 -0.17 -0.01 -0.04 -0.04 ...
## $ Maths.education.science.score : num 525 496 515 526 494 ...
## $ Child.conflict : num NA 0.31 0.33 0.24 -0.14 -1.25 0.59 -0.7 0.4 -0.06 ...
## $ Foreign.aid : num 0.25 0.52 0.53 0.34 0.81 0.47 0.47 0.35 0.24 0.41 ...
## $ Recycling : num 7.4 NA NA NA NA NA 6 3.4 NA NA ...
## $ Peace.index : num 1.66 1.48 1.49 1.48 1.38 1.45 1.73 1.52 1.79 1.4 ...
## $ Maternity.leave : int 0 16 15 17 18 18 16 14 17 18 ...
## $ Advertising : num 1.24 0.97 0.82 0.77 0.75 0.9 0.71 0.99 1.04 1 ...
## $ Police : int 304 305 357 186 192 160 NA 303 NA NA ...
## $ Social.expenditure : num 17.8 27.5 26.5 17.2 27.6 25.8 29 27.3 19.9 15.8 ...
## $ Women.s_status : num 0.46 -0.81 0.61 0.56 0.83 1.08 -0.17 -0.21 -0.85 -0.21 ...
## $ Lone.parents : int 21 15 12 17 22 19 12 21 3 14 ...
## $ GDP_WB : int 45926 47682 43435 45066 45537 40676 39328 46401 26851 49393 ...
이 자료 중 소득불평등을 나타내는 지표는 5분위계수로서 두번째 컬럼에 Income.inequality라는 이름으로 나와 있고, 건강과 사회문제 지표는 13번째 컬럼에 Index.of.health...social_problems라는 이름으로 주어져 있다. 나라들은 Country라는 변수명으로 첫번째 컬럼에 나와 있다.
# par(mai = c(2.0, 0.8, 0.8, 0.4) + 0.02)
library(ggplot2)
o.ineq <- order(data.full$Income.inequality)
fifth_ratio <- data.full$Income.inequality
Country <- data.full$Country
data.full$Country.o <- factor(data.full$Country, levels = Country[o.ineq])
g1 <- ggplot(data = data.full[c("Income.inequality", "Country.o")])
g2 <- g1 + geom_bar(aes(x = Country.o, y = Income.inequality), stat = "identity", width = 0.8, fill = rev(rainbow(23, start = 0, end = 2/3)))
g2
g3 <- g2 +
theme_bw()
g4 <- g3 +
# theme(axis.text.x = element_blank()) +
theme(axis.ticks.x = element_blank()) +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
g4
g5 <- g4 +
geom_text(aes(x = Country.o, y = Income.inequality + 0.2, label = format(Income.inequality, digits = 3)))
g5
g6 <- g5 +
labs(title = "Income Inequality (Fifth Ratio)", x = "Countries", y = "Fifth Ratio")
g6
ggsave("../pics/Fifth_Ratio_ggplot.png", g6, width = 12, height = 6)
그리고, 건강과 사회문제 지표에 결측치들이 있기 때문에 먼저 이 나라들을 제외하고 분석작업을 수행하여야 한다. which()를 이용하여 해당 인덱스를 찾고, 나라명을 추출한다.
(country.na <- which(is.na(data.full$Index.of.health...social_problems)))
## [1] 11 18
data.full$Country[country.na]
## [1] "Israel" "Singapore"
결측치가 있는 나라를 빼고, 필요한 변수만 챙겨서 새로운 data frame 을 구성하기 위하여 건강과 사회문제 지표의 위치를 찾아보자.
names(data.full)
## [1] "Country"
## [2] "Income.inequality"
## [3] "Trust"
## [4] "Life.expectancy"
## [5] "Infant.mortality"
## [6] "Obesity"
## [7] "Mental.illness"
## [8] "Maths.and.literacy.scores"
## [9] "Teenage.births"
## [10] "Homicides"
## [11] "Imprisonment..log."
## [12] "Social.mobility"
## [13] "Index.of.health...social_problems"
## [14] "Child.overweight"
## [15] "Drugs.index"
## [16] "Calorie.intake"
## [17] "Public.health.expenditure"
## [18] "Child.wellbeing"
## [19] "Maths.education.science.score"
## [20] "Child.conflict"
## [21] "Foreign.aid"
## [22] "Recycling"
## [23] "Peace.index"
## [24] "Maternity.leave"
## [25] "Advertising"
## [26] "Police"
## [27] "Social.expenditure"
## [28] "Women.s_status"
## [29] "Lone.parents"
## [30] "GDP_WB"
## [31] "Country.o"
which(names(data.full) == "Index.of.health...social_problems")
## [1] 13
새로운 data frame 을 data.21 으로 저장하자. 시각적 가독성을 높이기 위하여 자릿수를 조정한다.
options(digits = 2)
v.names <- c("Country", "Income.inequality", "Index.of.health...social_problems", "GDP_WB")
data.21 <- data.full[-c(11, 18), v.names]
names(data.21)[3] <- "Index.HS"
kable(data.21)
| Country | Income.inequality | Index.HS | GDP_WB | |
|---|---|---|---|---|
| 1 | Australia | 7.0 | 0.07 | 45926 |
| 2 | Austria | 4.8 | 0.01 | 47682 |
| 3 | Belgium | 4.6 | -0.23 | 43435 |
| 4 | Canada | 5.6 | -0.07 | 45066 |
| 5 | Denmark | 4.3 | -0.19 | 45537 |
| 6 | Finland | 3.7 | -0.43 | 40676 |
| 7 | France | 5.6 | 0.05 | 39328 |
| 8 | Germany | 5.2 | -0.06 | 46401 |
| 9 | Greece | 6.2 | 0.38 | 26851 |
| 10 | Ireland | 6.0 | 0.25 | 49393 |
| 12 | Italy | 6.7 | -0.12 | 35463 |
| 13 | Japan | 3.4 | -1.26 | 36319 |
| 14 | Netherlands | 5.3 | -0.51 | 48253 |
| 15 | New Zealand | 6.8 | 0.29 | 37679 |
| 16 | Norway | 3.9 | -0.63 | 65615 |
| 17 | Portugal | 8.0 | 1.18 | 28760 |
| 19 | Spain | 5.5 | -0.30 | 33629 |
| 20 | Sweden | 4.0 | -0.83 | 45297 |
| 21 | Switzerland | 5.7 | -0.46 | 59540 |
| 22 | UK | 7.2 | 0.79 | 40233 |
| 23 | USA | 8.6 | 2.02 | 54630 |
우선 소득불평등과 건강 및 사회문제 지표의 관계를 대략적으로 살펴보면,
Index_inequality.df <- data.21[c("Country", "Income.inequality", "Index.HS")]
# plot(Index_inequality.df)
# plot(data.21[c("Income.inequality", "Index.HS")])
cor.1 <- cor(data.21["Income.inequality"], data.21["Index.HS"])
cor.1
## Index.HS
## Income.inequality 0.87
p1 <- ggplot(data = Index_inequality.df, aes(x = Income.inequality, y = Index.HS, label = Country)) + theme_bw()
p2 <- p1 + geom_point(col = "red")
p2
p3 <- p2 + geom_text()
p3
텍스트 위치를 옮겨 보자. 점의 왼편으로 옮겨야 할 나라들(ggplot에서는 hjust = "right")을 먼저 찾아보자.
Country <- data.21$Country
which(Country %in% c("Austria", "Denmark", "Germany", "Netherlands"))
## [1] 2 5 8 13
text.left <- which(Country %in% c("Austria", "Denmark", "Germany", "Netherlands"))
text.left
## [1] 2 5 8 13
text.right <- setdiff(1:nrow(data.21), text.left)
text.right
## [1] 1 3 4 6 7 9 10 11 12 14 15 16 17 18 19 20 21
hjust.text <- ifelse(1:nrow(data.21) %in% text.left, "right", "left")
p4 <- p2 + geom_text(hjust = hjust.text)
p4
독일의 라벨을 위로 붙이면 보기가 나아질 것으로 생각되므로,
which(Country %in% "Germany")
## [1] 8
text.up <- which(Country %in% "Germany")
text.up
## [1] 8
text.left <- setdiff(1:nrow(data.21), c(text.right, text.up))
text.left
## [1] 2 5 13
pos.text <- ifelse(1:nrow(data.21) %in% text.up, 3, ifelse(1:nrow(data.21) %in% text.left, 2, 4))
vjust.text <- ifelse(1:nrow(data.21) %in% text.up, "bottom", "center")
hjust.text <- ifelse(1:nrow(data.21) %in% text.up, "middle", hjust.text)
p5 <- p2 + geom_text(hjust = hjust.text, vjust = vjust.text)
p5
세부조정을 위해서 nudge_x, nudge_y 설정
nudge_y.text <- ifelse(vjust.text == "bottom", 0.05, 0)
nudge_x.text <- ifelse(hjust.text == "middle", 0, ifelse(hjust.text == "right", -0.05, 0.05))
p6 <- p2 + geom_text(hjust = hjust.text, vjust = vjust.text, nudge_x = nudge_x.text, nudge_y = nudge_y.text)
p6
건강 및 사회문제 지표의 경우 어느 방향이 좋은지 알 수 없으므로 친절하게 도표의 주변에(margin)에 알려주고, 이제 조정된 text 외에 x-축과 y-축에 적절한 라벨과 메인 타이틀을 넣어보자.
main.title <- "Income Inequality vs Index of Health and Social Problems"
x.lab <- "Income Inequality (Fifth Ratio)"
y.lab <- "Index of Health and Social Problems"
lowest <- data.21$Income.inequality[Country == "Japan"]
highest <- data.21$Income.inequality[Country == "USA"]
p7 <- p6 +
scale_x_continuous(name = x.lab, breaks = c(lowest, 4:8, highest), labels = c(lowest, 4:8, highest), limits = c(3, 9)) +
scale_y_continuous(name = y.lab, breaks = c(-1.5, -1:2, 2.5), labels = c(expression("Better" %down% ""), -1:2, expression("Worse" %up% "")), limits = c(-1.5, 2.5)) +
labs(title = main.title)
p7
상관계수를 텍스트로 그림 안에 넣어주고 여기까지 작업한 내용을 별도의 파일로 저장해 놓으려면,
p8 <- p7 + annotate("text", x = 5, y = 1.5, label = paste("r =", round(cor.1, digits = 2)), size = 5)
p8
선형회귀선을 추가하여 대체적인 추세를 보려면 lm()을 이용하되, x, y의 순서를 제대로 바꿔야 함에 유의.
lm.ineq <- lm(Index.HS ~ Income.inequality, data = Index_inequality.df)
# lm.ineq <- lm(Index_inequality.df[2:1])
# p9 <- p8 + geom_abline(intercept = lm.ineq$coef[1], slope = lm.ineq$coef[2], colour = "blue")
p9 <- p8 + geom_smooth(method = "loess", se = FALSE, colour = "blue")
p9
ggsave("../pics/Inequality_vs_HS_Index_ggplot.png", p9, width = 8, height = 6)
Index_GDP.df <- data.21[c("GDP_WB", "Index.HS")]
text.left.2 <- which(Country %in% c("Canada", "Belgium", "Australia"))
text.right.2 <- setdiff(1:nrow(data.21), c(text.left.2))
pos.text.2 <- ifelse(1:nrow(data.21) %in% text.left.2, 2, 4)
plot(Index_GDP.df, pch = 20, col = "red", xlim = c(25000, 70000), ylim = c(-1.5, 2.5), xaxt = "n", ann = FALSE)
axis(side = 1, at = seq(30000, 70000, by = 10000), labels = paste(3:7, "만", sep = ""))
text(Index_GDP.df, labels = Country, pos = pos.text.2, cex = 0.8)
# cor.2 <- cor(Index_GDP.df["GDP_WB"], Index_GDP.df["Index.HS"])
cor.2 <- cor(Index_GDP.df[1], Index_GDP.df[2])
text(x = 40000, y = 2, labels = paste("r =", round(cor.2, digits = 2)), cex = 1.2)
main.title.2 <- "GDP vs Index of Health and Social Problems"
x.lab.2 <- "GDP (Thousand Dollars)"
y.lab.2 <- "Index of Health and Social Problems"
title(main = main.title.2, xlab = x.lab.2, ylab = y.lab.2)
mtext(c("Better", "Worse"), side = 2, at = c(-1.8, 2.8), las = 1)
# dev.copy(png, file = "../pics/GDP_health_social_en_72dpi.png", width = 640, height = 480)
# dev.off()
GDP와의 관계
# par(family = "HCR Dotum LVT")
Index_GDP.df <- data.21[c("GDP_WB", "Index.HS")]
text.left.2 <- which(Country %in% c("Canada", "Belgium", "Australia"))
text.right.2 <- setdiff(1:nrow(data.21), c(text.left.2))
pos.text.2 <- ifelse(1:nrow(data.21) %in% text.left.2, 2, 4)
plot(Index_GDP.df, pch = 20, col = "red", xlim = c(25000, 70000), ylim = c(-1.5, 2.5), xaxt = "n", ann = FALSE)
axis(side = 1, at = seq(30000, 70000, by = 10000), labels = paste(3:7, "만", sep = ""))
text(Index_GDP.df, labels = Country.kr, pos = pos.text.2, cex = 0.8)
text(x = 40000, y = 2, labels = paste("r =", round(cor.1, digits = 2)), cex = 1.2)
main.title.2.kr <- "GDP와 건강 및 사회문제 지수"
x.lab.2.kr <- "GDP(달러)"
y.lab.2.kr <- "건강 및 사회문제 지수"
title(main = main.title.2.kr, xlab = x.lab.2.kr, ylab = y.lab.2.kr)
mtext(c("좋음", "나쁨"), side = 2, at = c(-1.8, 2.8), las = 1)
# dev.copy(png, file = "../pics/GDP_health_social_72dpi.png", width = 640, height = 480)
# dev.off()
–>
data.usa <- read.xlsx("../data/USA-inequality.xls", 1, stringsAsFactors = FALSE)
str(data.usa)
## 'data.frame': 50 obs. of 20 variables:
## $ State : chr "Alabama" "Alaska" "Arizona" "Arkansas" ...
## $ State.Abbrev : chr "AL" "AK" "AZ" "AR" ...
## $ Income.Inequality : num 0.475 0.402 0.45 0.458 0.475 ...
## $ Trust : num 23 NA 47 29 43 46 49 NA 37 38 ...
## $ Life.expectancy : num 74.6 76.7 77.5 75.1 78.3 ...
## $ Infant.mortality : num 9.1 5.5 6.4 8.3 5.5 ...
## $ Obesity : num 32 30 28.5 31 31 21.5 26.5 27 27.5 30.5 ...
## $ Mental.health : num 3.3 2.8 2.2 3.2 3.3 ...
## $ Maths.and.literacy.scores : num 258 268 263 262 259 ...
## $ Teenage.births : num 62.9 42.4 69.1 68.5 48.5 ...
## $ Homicides : num 78.9 85.6 80.4 56.1 60.5 ...
## $ Imprisonment : num 509 413 507 415 478 357 372 429 447 502 ...
## $ Index.of.health...social.problems: num 1.385 0.137 0.212 0.948 0.327 ...
## $ Overweight.children : num 35 31 30 33 30 22 27 35 32 32 ...
## $ Child.wellbeing : num 8.5 4.4 4.9 9.3 -3.4 ...
## $ Women.s.status : num -0.932 0.74 -0.147 -1.318 0.969 ...
## $ Juvenile.homicides : num 12 8 7 6 10 4 4 0 NA 8 ...
## $ High.school.drop.outs : num 24.7 11.7 19 24.7 23.2 ...
## $ Child.mental.illness : num 11.5 8.2 8.7 11.8 7.5 ...
## $ Pugnacity : num 41.8 NA 36.3 38.4 37.7 ...
당장 필요한 변수들만 모아서 data frame으로 재구성한다. 변수명 설정에 유의한다.
data.usa.1 <- data.frame(Gini = data.usa$Income.Inequality, HS.index = data.usa$Index.of.health...social.problems)
str(data.usa.1)
## 'data.frame': 50 obs. of 2 variables:
## $ Gini : num 0.475 0.402 0.45 0.458 0.475 ...
## $ HS.index: num 1.385 0.137 0.212 0.948 0.327 ...
Gini <- data.usa.1$Gini
State <- data.usa$State
Abb <- data.usa$State.Abbrev
options(digits = 3)
kable(data.frame(State = State, State.Abb = Abb, data.usa.1))
| State | State.Abb | Gini | HS.index |
|---|---|---|---|
| Alabama | AL | 0.475 | 1.385 |
| Alaska | AK | 0.402 | 0.137 |
| Arizona | AZ | 0.450 | 0.212 |
| Arkansas | AR | 0.458 | 0.948 |
| California | CA | 0.475 | 0.327 |
| Colorado | CO | 0.438 | -0.507 |
| Connecticut | CT | 0.477 | -0.660 |
| Delaware | DE | 0.429 | 0.133 |
| Florida | FL | 0.470 | 0.360 |
| Georgia | GA | 0.461 | 0.896 |
| Hawaii | HI | 0.434 | -0.388 |
| Idaho | ID | 0.427 | -0.429 |
| Illinois | IL | 0.456 | 0.206 |
| Indiana | IN | 0.424 | 0.370 |
| Iowa | IA | 0.418 | -0.895 |
| Kansas | KS | 0.435 | -0.442 |
| Kentucky | KY | 0.468 | 0.874 |
| Louisiana | LA | 0.483 | 1.595 |
| Maine | ME | 0.434 | -0.769 |
| Maryland | MD | 0.434 | 0.187 |
| Massachusetts | MA | 0.463 | -0.959 |
| Michigan | MI | 0.440 | 0.349 |
| Minnesota | MN | 0.426 | -1.216 |
| Mississippi | MS | 0.478 | 1.692 |
| Missouri | MO | 0.449 | 0.392 |
| Montana | MT | 0.436 | -0.906 |
| Nebraska | NE | 0.424 | -0.583 |
| Nevada | NV | 0.436 | 0.803 |
| New Hampshire | NH | 0.414 | -1.242 |
| New Jersey | NJ | 0.460 | -0.402 |
| New Mexico | NM | 0.460 | 0.564 |
| New York | NY | 0.499 | -0.179 |
| North Carolina | NC | 0.452 | 0.494 |
| North Dakota | ND | 0.429 | -1.145 |
| Ohio | OH | 0.441 | 0.058 |
| Oklahoma | OK | 0.455 | 0.494 |
| Oregon | OR | 0.438 | -0.346 |
| Pennsylvania | PA | 0.452 | -0.015 |
| Rhode Island | RI | 0.457 | -0.389 |
| South Carolina | SC | 0.454 | 0.899 |
| South Dakota | SD | 0.434 | -0.759 |
| Tennessee | TN | 0.465 | 0.788 |
| Texas | TX | 0.470 | 0.930 |
| Utah | UT | 0.410 | -0.709 |
| Vermont | VT | 0.423 | -1.183 |
| Virginia | VA | 0.449 | -0.055 |
| Washington | WA | 0.436 | -0.516 |
| West Virginia | WV | 0.468 | 0.482 |
| Wisconsin | WI | 0.413 | -0.473 |
| Wyoming | WY | 0.428 | -0.551 |
주별 Gini계수를 barplot()으로 비교해 보자. 전부 0.4는 넘고 0.5는 넘지 않기 때문에 차이를 살피기 위해서 y축의 범위(ylim =)를 조정하였다. 이때 xpd = FALSE가 어떤 역할을 하는지 잘 알아두자.
par(mai = c(2.0, 0.8, 0.8, 0.4) + 0.2)
o.Gini <- order(Gini)
b.Gini <- barplot(Gini[o.Gini], names.arg = State[o.Gini], col = rev(rainbow(50, start = 0, end = 4/6)), ylim = c(0.3, 0.52), xpd = FALSE, las = 2)
text(x = b.Gini[c(1, 25, 50)], y = Gini[o.Gini][c(1, 25, 50)] + 0.01, labels = format(Gini[o.Gini][c(1, 25, 50)], digits = 3))
title(main = "Gini Coefficients of United States")
간단한 산점도를 그리고, 추가 작업을 생각한다.
plot(data.usa.1)
x-축과 y-축의 범위를 설정하고, pch = 20으로 다시 그린다.
plot(data.usa.1, pch = 20, xlim = c(0.39, 0.51), ylim = c(-1.5, 2.0))
각 주의 약칭을 새겨넣는다.
plot(data.usa.1, pch = 20, xlim = c(0.39, 0.51), ylim = c(-1.5, 2.0))
text(data.usa.1, labels = Abb, pos = 4)
겹쳐보이는 주의 약칭들로부터 인덱스를 추출한다.
which(Abb %in% c("VT", "ME", "NE", "WA", "VA", "HI", "RI", "SC", "AR", "NC", "GA", "KY"))
## [1] 4 10 11 17 19 27 33 39 40 45 46 47
점 왼쪽에 약칭을 넣을 주들의 인덱스를 저장한다. 나머지 인덱스는 오른쪽에 넣을 것으로 따로 저장한다.
text.left.us <- which(Abb %in% c("VT", "ME", "NE", "WA", "VA", "HI", "RI", "SC", "AR", "NC", "GA", "KY"))
text.right.us <- setdiff(1:nrow(data.usa.1), text.left.us)
pos.text.us <- ifelse(1:nrow(data.usa.1) %in% text.left.us, 2, 4)
왼쪽, 오른쪽 위치를 조정한 주 약칭을 다시 넣는다.
plot(data.usa.1, pch = 20, col = "red", xlim = c(0.39, 0.51), ylim = c(-1.5, 2.0))
text(data.usa.1, labels = Abb, pos = pos.text.us)
점 아래에 약칭을 넣을 주들의 인덱스를 찾는다. 왼쪽 인덱스, 오른쪽 인덱스에서 조정한다.
text.down.us <- which(Abb %in% c("WA", "AR", "GA", "MN"))
which(text.left.us %in% text.down.us)
## [1] 1 2 12
text.left.us <- setdiff(text.left.us, text.down.us)
text.right.us <- setdiff(text.right.us, text.down.us)
pos.text.us <- ifelse(1:nrow(data.usa.1) %in% text.down.us, 1, ifelse(1:nrow(data.usa.1) %in% text.left.us, 2, 4))
약칭 위치를 아래로 조정한 산점도를 다시 그린다.
plot(data.usa.1, pch = 20, col = "red", xlim = c(0.39, 0.51), ylim = c(-1.5, 2.0))
text(data.usa.1, labels = Abb, pos = pos.text.us)
상관계수를 추가한다.
plot(data.usa.1, pch = 20, col = "red", xlim = c(0.39, 0.51), ylim = c(-1.5, 2.0))
text(data.usa.1, labels = Abb, pos = pos.text.us)
cor.us <- cor(data.usa.1$HS.index, data.usa.1$Gini)
text(x = 0.42, y = 1.5, labels = paste("r =", round(cor.us, digits = 2)), col = "red", cex = 1.2)
단순회귀선을 추가한다.
plot(data.usa.1, pch = 20, col = "red", xlim = c(0.39, 0.51), ylim = c(-1.5, 2.0))
text(data.usa.1, labels = Abb, pos = pos.text.us)
text(x = 0.42, y = 1.5, labels = paste("r =", round(cor.us, digits = 2)), col = "red", cex = 1.2)
# lm.ineq.us <- lm(HS.index ~ Gini, data = data.usa.1)
lm.ineq.us <- lm(data.usa.1[2:1])
abline(lm.ineq.us$coef, col = "blue")
# abline(lm(HS.index ~ Gini, data = data.usa.1)$coef)
주제목을 추가하고, xlab, ylab을 수정한다. 수직축의 의미를 명확히 한다.
plot(data.usa.1, pch = 20, col = "red", xlim = c(0.39, 0.51), ylim = c(-1.5, 2.0), ann = FALSE)
text(data.usa.1, labels = Abb, pos = pos.text.us)
text(x = 0.42, y = 1.5, labels = paste("r =", round(cor.us, digits = 2)), col = "red", cex = 1.2)
abline(lm.ineq.us$coef, col = "blue")
mtext(c("Better", "Worse"), side = 2, at = c(-1.8, 2.3), las = 1)
main.title.us <- "Income Inequality vs Health and Social Index (USA)"
x.lab.us <- "Gini Coefficients"
y.lab.us <- "Index of Health and Social Problems"
title(main = main.title.us, xlab = x.lab.us, ylab = y.lab.us)